/*
 页面名称：管理员 -添加员工
 作者: 苗怀雨
 时间:2017年12月31日
 最后编辑：2017年12月31日20:20:13
 */

$(document).ready(function() {
    var buttons = $(".navbar-right").find("button.btn");

    /*管理员获得对应的借阅列表按钮*/
    buttons.eq(0).click(function () {
        toAllBorrowList();
    });
    buttons.eq(1).click(function () {
        toOrderBorrowList();
    });
    buttons.eq(2).click(function () {
        toBorrowedBorrowList();
    });
    buttons.eq(3).click(function () {
        toReturnBorrowList();
    });
    buttons.eq(4).click(function () {
        toCompleteBorrowList();
    });
    buttons.eq(5).click(function () {
        toPostDateBorrowList();
    });
    buttons.eq(6).click(function () {
        toLostBorrowList();
    });
});

function borrowList(e,type) {
    var $this = $(e);
    var pageNum = $this.text();
    var pageSize = $("#pageSize").val();
    var $type = type;

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    if($type == 0){
        $elem.attr("id","pageNum");
        $elem.text(pageNum);
        $form.append($elem.clone());

        $elem.attr("id","pageSize");
        $elem.text(pageSize);
        $form.append($elem.clone());

        postForm("allBorrowListPage",$form);
    }else{
        $elem.attr("id","pageNum");
        $elem.text(pageNum);
        $form.append($elem.clone());

        $elem.attr("id","pageSize");
        $elem.text(pageSize);
        $form.append($elem.clone());

        $elem.attr("id","type");
        $elem.text($type);
        $form.append($elem.clone());

        postForm("borrowListPageByState",$form);
    }
}

function ensureBorrow(e) {
    var $modal = $(".bs-example-modal-eb");
    var $tableElemenets = $(e).parents("tr").children('td');
    var bookName = $tableElemenets.eq(5).text();
    var borrowId = $tableElemenets.eq(1).text();
    var staffName = $tableElemenets.eq(3).text();
    var type = $tableElemenets.eq(13).children("span").text();

    $("h4",$modal).eq(1).text("确认操作:员工"+ staffName + "  " + type.substr(0,2) + "《"+bookName+"》");       //设置标题名称
    $(".btn-primary",$modal).attr("onclick","ensure("+borrowId+");");  //设置确定按钮
}

//确认操作
function ensure(borrowId) {
    var $modal = $(".bs-example-modal-eb");
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","borrowId");
    $elem.text(borrowId);
    $form.append($elem.clone());

    $elem.attr("id","type");
    $elem.text(0);
    $form.append($elem.clone());

    $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用

    postForm("ensureBorrow",$form);
}

function dealInfo(e,type,admin) {
    var $modal = $(".bs-example-modal-lb");
    var $titles = $("h4",$modal);
    var $tableElemenets = $(e).parents("tr").children('td');
    var bookName = $tableElemenets.eq(5).text();
    var borrowId = $tableElemenets.eq(1).text();
    var staffId = $tableElemenets.eq(2).text();
    var staffName = $tableElemenets.eq(3).text();
    var startTime = $tableElemenets.eq(10).text();
    var endTime = $tableElemenets.eq(11).text();
    var borrowDays = $tableElemenets.eq(12).text();
    var allDays = $tableElemenets.eq(13).text();
    var bookPrice = $tableElemenets.eq(16).text();
    var date = new Date();

    if(type == "5"){      //挂失
        $titles.eq(0).text("挂失处理");
        $titles.eq(1).text("挂失申请");       //设置标题名称
        $titles.eq(1).children("span").text(staffName);
        $titles.eq(1).prepend("<span class='green'>"+staffName +"</span>");
        $titles.eq(2).children("span").text("《"+bookName + "》");

        if(borrowDays >= allDays){
            $titles.eq(3).children("span").text(1.0 * bookPrice + 10.0 + (1.0*borrowDays - 1.0*allDays) * 0.3);
        }else{
            $titles.eq(3).children("span").text(1.0 * bookPrice + 10.0);
        }

        $titles.eq(4).children("span").text(admin);
        $titles.eq(5).children("span").text(date.toLocaleDateString());
        $titles.eq(7).children("span").text(borrowId);
        $titles.eq(8).children("span").text(staffId);
    }else if(type == "1"){  //催还
        var $modal = $(".bs-example-modal-eb");
        var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
        var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

        var $form = $("<div class='postForm'></div>");
        var $elem = $("<div class='postParam'></div>");

        $elem.attr("id","borrowId");
        $elem.text(borrowId);
        $form.append($elem.clone());

        $elem.attr("id","type");
        $elem.text(3);
        $form.append($elem.clone());

        $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用

        postForm("ensureBorrow",$form);
    } else{      //超期
        $titles.eq(0).text("超期处理");
        $titles.eq(1).text("超期处理申请");       //设置标题名称
        $titles.eq(1).children("span").text(staffName);
        $titles.eq(1).prepend("<span class='green'>"+staffName +"</span>");

        $titles.eq(2).children("span").text("《"+bookName + "》");
        $titles.eq(3).children("span").text(0.3* (1.0*borrowDays - 1.0*allDays));
        $titles.eq(4).children("span").text(admin);
        $titles.eq(5).children("span").text(date.toLocaleDateString());

        $titles.eq(6).children("span").text(1.0 * borrowDays - 1.0* allDays);
        $titles.eq(6).css("display","block");

        $titles.eq(7).children("span").text(borrowId);
        $titles.eq(8).children("span").text(staffId);
        $titles.eq(9).children("span").text("0");
    }

    $(".btn-primary",$modal).attr("onclick","deal("+borrowId+");");  //设置确定按钮
}

//确认操作
function deal(borrowId) {
    var $modal = $(".bs-example-modal-lb");
    var $closeButton = $("button",$modal).eq(1);        //弹出框关闭按钮
    var $ensureButton = $("button",$modal).eq(2);       //弹出框确定按钮

    $ensureButton.attr("disabled","disabled"); //设置编辑确定按钮暂时不可用
    postForm("deal",$modal.find(".postForm"));
}

function checkEvaluate(e) {
    var $this = $(e);
    var $tableElemenets = $this.parents("tr").children('td');
    var bookId = $tableElemenets.eq(17).text();
    var staffId = $tableElemenets.eq(2).text();

    var $form = $("<div class='postForm'></div>");
    var $elem = $("<div class='postParam'></div>");

    $elem.attr("id","bookId");
    $elem.text(bookId);
    $form.append($elem.clone());

    $elem.attr("id","staffId");
    $elem.text(staffId);
    $form.append($elem.clone());

    postForm("checkEvaluatePage",$form);
}

/*催还图书*/
function urgeReturn(e,borrowId) {
    $(e).removeAttr("onclick");
    $.ajax({
        type:'POST',//请求类型
        url:'urgeReturnBook',//请求地址
        contentType: 'application/json;',//发送信息至服务器时内容编码类型
        data:JSON.stringify({
            "borrowId":borrowId,
        }),
        dataType:'JSON',/*预期服务器返回的数据类型*/
        success:function (json) {//成功获取到后台返回的值后，对后台返回的值做相应的一系列操作
            if(json.result == false){
                createPNotify("提示信息","error",json.err_msg);
            }else{
                createPNotify("提示信息","success","催还成功!");
                $(e).attr("onclick","urgeReturn(this,"+borrowId + ")");
            }
        },
        cache:true,
    });
}


function bigImage(src) {
    var $modal = $("div.bigImage");
    var imageSrc = src;

    if(imageSrc == "/library/"){
        imageSrc = "/library/assets/images/no-image.jpg";
    }

    $modal.find("img").attr("src",imageSrc);
}
